from fastapi_utils.api_model import APIModel
from pydantic import BaseModel, Field, ConfigDict
from datetime import datetime
from typing import Optional, List


# 权限models序列化类
class PowerOutSchema(APIModel):
    id: int = Field(description="权限ID")
    name: str = Field(description="权限名称")
    type: str = Field(description="权限类型")
    code: str = Field(description="权限标识")
    url: Optional[str] = Field(description="权限链接")
    open_type: Optional[str] = Field(description="打开方式")
    parent_id: int = Field(description="父级权限ID")
    icon: Optional[str] = Field(description="图标")
    sort: int = Field(description="排序")
    create_time: datetime = Field(description="创建时间")
    update_time: datetime = Field(description="更新时间")
    enable: int = Field(description="是否启用")

    model_config = ConfigDict(from_attributes=True)


class MenuOut(BaseModel):
    id: int = Field(description="权限ID")
    title: str = Field(description="权限名称", alias="name")
    type: str = Field(description="权限类型")
    code: str = Field(description="权限标识")
    href: Optional[str] = Field(description="权限链接", alias="url")
    openType: Optional[str] = Field(description="打开方式", alias="open_type")
    parent_id: int = Field(description="父级权限ID")
    icon: Optional[str] = Field(description="图标")
    sort: int = Field(description="排序")
    create_time: datetime = Field(description="创建时间")
    update_time: datetime = Field(description="更新时间")
    enable: int = Field(description="是否启用")

    model_config = ConfigDict(from_attributes=True)


class PowerTree(PowerOutSchema):
    children: List["PowerTree"] = []


class PowerBaseSchema(APIModel):
    icon: Optional[str] = Field(None, description="图标")
    open_type: Optional[str] = Field(None, description="打开方式", alias="openType")
    parent_id: int = Field(0, description="父级权限ID", alias="parentId")
    code: str = Field(..., description="权限标识", alias="powerCode")
    name: str = Field(..., description="权限名称", alias="powerName")
    type: str = Field(..., description="权限类型", alias="powerType")
    url: Optional[str] = Field(None, description="权限路径", alias="powerUrl")
    sort: int = Field(0, description="排序")


class PowerSaveSchema(PowerBaseSchema):
    pass


class PowerUpdateSchema(PowerBaseSchema):
    powerId: int = Field(..., description="权限ID")


class PowerEnableDisableSchema(BaseModel):
    powerId: int = Field(..., description="权限ID")

    model_config = ConfigDict(from_attributes=True)
